package com.wow.wa.bill.util;

import com.wow.wa.bill.vo.Card;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	static String DATABASE_NAME = "BILL_DB";

	private String CREATE_CARD_SQL = "CREATE TABLE Card ("
			+ "_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"
			+ "note text NOT NULL," + "seq text NOT NULL,"
			+ "type text NOT NULL," + "name text NOT NULL,"
			+ "amount text NOT NULL);";

	private String CREATE_TABLE_BILL_SQL = "CREATE TABLE Bill ("
			+ "_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"
			+ " name text NOT NULL," + "type text NOT NULL,"
			+ "isPayed text NOT NULL," + "createDate text NOT NULL,"
			+ "dueDate text NOT NULL," 
			+ "dollar text NOT NULL," + "note text NOT NULL,"
			+ "isShowPayed text NOT NULL);";

	private String CREATE_TABLE_CATEGORY_SQL = " CREATE TABLE Category ("
			+ "_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"
			+ "name text NOT NULL," + "cardId text NOT NULL );";

	private String CREATE_TANLE_CARDTYPE_SQL = "CREATE TABLE CardType ("
			+ " _id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"
			+ " name text NOT NULL, " + " cardId text NOT NULL );";

	private String CREATE_TABLE_BROADCAST_REC = "CREATE TABLE BroadcastRec ("
			+ "_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,"
			+ "yyymmdd text NOT NULL," + "hhmmss text NOT NULL" + ");";

	String DROP_TABLE_BILL_SQL = "DROP TABLE IF EXISTS Bill";
	String DROP_TABLE_CATEGORY_SQL = "DROP TABLE IF EXISTS Category";
	String DROP_TABLE_CARD_SQL = "DROP TABLE IF EXISTS Card";
	String DROP_TABLE_CARD_TYPE_SQL = "DROP TABLE IF EXISTS CardType";
	String DROP_TABLE_BROADCAST_REC_SQL = "DROP TABLE IF EXISTS BroadcastRec ";

	static int DATABASE_VERSION = 24;

	private Context ct;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.ct = context;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		db.execSQL(CREATE_CARD_SQL);
		db.execSQL(CREATE_TABLE_BILL_SQL);
		db.execSQL(CREATE_TABLE_CATEGORY_SQL);
		db.execSQL(CREATE_TANLE_CARDTYPE_SQL);
		db.execSQL(CREATE_TABLE_BROADCAST_REC);
		
		
		final Card card=new Card();
		card.setAmount("6000");
		card.setName("生活費");
		card.setNote("系統產生");
		card.setSeq("0");
		card.setType("0");
		
		VoConvertComponent voComponet=new VoConvertComponentImpl();
		try {
			
			;
			
			db.insert(Args.DBSchema.BILL_TABLE_NAME, "", voComponet.contentValueFromVo(card));
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		// db.execSQL("INSERT INTO `Bill` (`_id`,`name`,`type`,`isPayed`,`createDate`,`dueDate`,`categoryId`,`dollar`,`note`,`isShowPayed`,`purchasingDay`) VALUES (1,'電話費','1','N','2013/10/29','2013/11/05','0','799','範例記錄，請刪除','Y','2013/11/05');");
		// db.execSQL("INSERT INTO `Bill` (`_id`,`name`,`type`,`isPayed`,`createDate`,`dueDate`,`categoryId`,`dollar`,`note`,`isShowPayed`,`purchasingDay`) VALUES (2,'治裝費','1','N','2013/10/29','2013/11/05','2','3000','範例記錄，請刪除','Y','2013/11/05')");
		// db.execSQL("INSERT INTO `Bill` (`_id`,`name`,`type`,`isPayed`,`createDate`,`dueDate`,`categoryId`,`dollar`,`note`,`isShowPayed`,`purchasingDay`) VALUES (3,'房租','3','N','','','3','6000','範例記錄，請刪除','Y','2013/11/05');");

		db.execSQL("INSERT INTO `Card` (`_id`,`note`,`seq`,`type`,`name`,`amount`) VALUES (1,'這是預設性信用卡類別，請修改名稱、繳費時間、收帳時間','1','0','生活類','6000');");
		
//		db.execSQL("INSERT INTO `Card` (`_id`,`note`,`seq`,`type`,`name`,`amount`) VALUES (1,'這是預設性信用卡類別，請修改名稱、繳費時間、收帳時間','1','0','花旗享樂卡','60000');");
//		db.execSQL("INSERT INTO `Card` (`_id`,`note`,`seq`,`type`,`name`,`amount`) VALUES (2,'這是預設性信用卡類別，請修改名稱、繳費時間、收帳時間','2','0','國泰信用卡','60000);");
//		db.execSQL("INSERT INTO `Card` (`_id`,`note`,`seq`,`type`,`name`,`amount`) VALUES (3,'這是現金類','0','1','現金','10000',);");

		

		// this.saveCATEGORY(this.ct, db);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onDropTable(db);
		onCreate(db);
	}

	public void onDropTable(SQLiteDatabase db) {

		db.execSQL(DROP_TABLE_BILL_SQL);
		db.execSQL(DROP_TABLE_CATEGORY_SQL);
		db.execSQL(DROP_TABLE_CARD_SQL);
		db.execSQL(DROP_TABLE_CARD_TYPE_SQL);
		db.execSQL(DROP_TABLE_BROADCAST_REC_SQL);
		

	}

}
